package indi.gtl.superbrain.activitys;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import indi.gtl.superbrain.R;
import indi.gtl.superbrain.adapter.ScanServicesAdapter;
import indi.gtl.superbrain.models.Constant;
import indi.gtl.superbrain.models.UserInfo;
import indi.gtl.superbrain.tools.NetTool;
import indi.gtl.superbrain.tools.NetToolClient;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;


public class ScanActivity extends Activity implements Constant {
    private static final String TAG = "ScanActivityClient";

    Button connectionButton;
    ListView serviceListView;
    List<String[]> servicesList;

    ScanServicesAdapter mAdapter;
    NetTool mNetTool;
    NetToolClient netToolClient;

    UserInfo mUserInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan);
        if(getActionBar() != null)
        	getActionBar().hide();

        connectionButton = (Button) findViewById(R.id.connectionButton);
        serviceListView = (ListView) findViewById(R.id.serviceListView);

        mUserInfo = new UserInfo();
        mUserInfo.setValue("Hehe", R.drawable.head_3, "990001020306700", "12345", 54, 32, 78, 23, 89, 23, 14560);

        mAdapter = new ScanServicesAdapter(ScanActivity.this, servicesList = new ArrayList<String[]>());
        serviceListView.setAdapter(mAdapter);
        serviceListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                new Thread() {
                    @Override
                    public void run() {
                        mNetTool = NetToolClient.connectService(((String[]) mAdapter.getItem(position))[0]);
                        if (mNetTool != null) {
                            // Toast.makeText(ScanActivity.this, "连接成功", Toast.LENGTH_LONG).show();
                            mNetTool.sendMessage(mUserInfo.toString());
                            mNetTool.setOnReceiveMessageListener(new NetTool.OnReceiveMessageListener() {
                                @Override
                                public void receiveMessage(String msg) {
                                    Log.d(TAG, msg);
                                    UserInfo enemyInfo = UserInfo.parse(msg);

                                    Intent intent = new Intent(ScanActivity.this, WaitClientActivity.class);
                                    intent.putExtra(KEY_MYUSER_INFO, mUserInfo);
                                    intent.putExtra(KEY_ENEMY_INFO, enemyInfo);
                                    startActivity(intent);
                                }

                                @Override
                                public void onDisconnect() {
                                    Toast.makeText(ScanActivity.this, "断开连接", Toast.LENGTH_LONG).show();
                                }
                            });
                        }
                    }
                }.start();
            }
        });

        netToolClient = new NetToolClient();
        netToolClient.setOnFindServiceListener(new NetToolClient.OnFindServiceListener() {
            @Override
            public void onFindAService(String ip, String msg) {
                servicesList.add(new String[]{ip, msg});
                mAdapter.updateList(servicesList);
            }

            @Override
            public void onScanOver(List<String> ipList) {

            }
        });
        netToolClient.scan();

        connectionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                netToolClient.scan();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mNetTool = NetTool.getNetTool();
        if(mNetTool != null)
            mNetTool.destroyConnection();
    }
}
